package cn.ly.link;

public class DoubleHeadLinkList<E>{
	private Node first;
	private Node last;
	
	public DoubleHeadLinkList(){
		this.first = null;
		this.last = null;
	}
	
	public void insertFirst(E e){
		Node node = new Node(e);
		if(isEmpty()){
			this.last = node;
		}
		node.next = first;
		first = node;
	}
	
	public void insertLast(E e){
		Node node = new Node(e);
		if(isEmpty()){
			first = node;         //如果链表是空的，则让第一个节点和最后一个节点都指向新的节点
		}else{
			this.last.next = node;//把最后一个节点的next指向新节点，让新节点变成最后一个节点的下一个节点
		}
		last = node;              //也就是新的最后一个节点
	}
	
	public Node deleteFirst(){
		Node node = first;
		if(first.next == null){
			last = null;
		}
		first = first.next;
		return node;
	}  
	
	public boolean isEmpty(){
		return first == null;
	}
	
	public void display(){
		Node cur = first;
		while(cur != null){
			cur.display();
			cur = cur.next;
		}
		System.out.println();
	}

}
